package com.wanwu.channel.common.enums;

import org.apache.commons.lang3.ArrayUtils;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author JiWen
 * @time 2020/1/19 11:47
 */
public enum PayChannelEnum {

    WeChat(1, "微信APP"),
    WeChatMN(2, "微信小程序"),
    AliPay(3, "支付宝App"),
    AliPayHK(4, "支付宝香港App"),

    AllianWeChat(5, "通联微信App"),
    AllianWeChatMN(6, "通联微信小程序"),
    AllianAliPay(7, "通联支付宝"),

    AllianClearPay(8,"通联跨境清分"),
    WeChatH5(9, "微信H5"),
    AllianWeChatH5(10, "通联H5"),
    AliPayH5(11, "支付宝H5")
    ;

    PayChannelEnum(Integer code, String desc) {
        this.code = code;
        this.desc = desc;
    }

    private Integer code;
    private String desc;

    public Integer getCode() {
        return code;
    }

    public String getDesc() {
        return desc;
    }


    public static PayChannelEnum getByCode(Integer code) {
        PayChannelEnum[] values = PayChannelEnum.values();
        for (PayChannelEnum payChannelEnum : values) {
            if (payChannelEnum.getCode().equals(code)) {
                return payChannelEnum;
            }
        }
        return null;
    }

    //通联渠道
    private static PayChannelEnum[] allinpayList = {AllianWeChat, AllianWeChatMN, AllianAliPay, AllianClearPay, AllianWeChatH5};

    public static Boolean isTongLianPay(Integer code) {
        PayChannelEnum payChannelEnum = getByCode(code);
        return ArrayUtils.contains(allinpayList, payChannelEnum);
    }

    //微信渠道
    private static PayChannelEnum[] wechatPayList = {WeChat, WeChatMN, WeChatH5};

    public static Boolean isWechatPay(Integer code) {
        PayChannelEnum payChannelEnum = getByCode(code);
        return ArrayUtils.contains(wechatPayList, payChannelEnum);
    }

    //支付宝渠道
    private static PayChannelEnum[] aliPayList = {AliPay, AliPayHK, AliPayH5};

    public static Boolean isAliPay(Integer code) {
        PayChannelEnum payChannelEnum = getByCode(code);
        return ArrayUtils.contains(aliPayList, payChannelEnum);
    }


    public static Integer getPayType(Integer code){
        // 用户支付的类型。1-APP 2-PC 3-扫码 4-其他
        if(PayChannelEnum.AllianWeChatMN.code.equals(code) || PayChannelEnum.AllianWeChat.code.equals(code)){
            return 3;
        }else if(PayChannelEnum.WeChat.code.equals(code) || PayChannelEnum.AliPay.code.equals(code) || PayChannelEnum.AliPayHK.code.equals(code)){
            return 1;
        }else{
            return 4;
        }
    }

    public static Map<String, String> toMap() {
        Map<String, String> map = new LinkedHashMap<>();
        for (PayChannelEnum value : values()) {
            map.put(String.valueOf(value.code), value.desc);
        }
        return map;
    }
}